package com.smrs.deliver.webapp.action;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.jof.framework.util.Pager;
import com.smrs.deliver.model.DeliverDetailModel;
import com.smrs.deliver.model.DeliverHeaderModel;
import com.smrs.enums.StatusEnum;
import com.smrs.goods.model.GoodsSkuModel;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;



public class DeliverDetailAction extends BaseDeliverAction{
	private static final long serialVersionUID = 1L;
	protected DeliverDetailModel deliverDetail ;
	protected DeliverHeaderModel deliverHeader ;
	protected GoodsSkuModel goodsSku;
	protected Pager<DeliverDetailModel> pager = new Pager<DeliverDetailModel>(); 
	protected String titleName="入库详细";

	
	public GoodsSkuModel getGoodsSku() {
		return goodsSku;
	}


	public void setGoodsSku(GoodsSkuModel goodsSku) {
		this.goodsSku = goodsSku;
	}


	public DeliverHeaderModel getDeliverHeader() {
		return deliverHeader;
	}


	public void setDeliverHeader(DeliverHeaderModel deliverHeader) {
		this.deliverHeader = deliverHeader;
	}

	public String addDeliverDetail(){		
		deliverDetail = new DeliverDetailModel();
		Pager<DeliverDetailModel> testPager = deliverDetailService.findBySkuAndDeliverHeader(goodsSku, deliverHeader,pager);
		goodsSku = goodsSkuService.getByPK(goodsSku.getId());
		//goodsSku = goodsSkuService.getByPK(goodsSku.getId());
		deliverHeader = deliverHeaderService.getByPK(deliverHeader.getId());
		List<DeliverDetailModel> list = testPager.getRecords();
		if(CollectionUtils.isNotEmpty(list)){
			this.addActionError(" sku:"+goodsSku.getName()+"  已经在入库单:"+ deliverHeader.getSeqNo() +"存在");
			return SUCCESS;
		}
		deliverDetail.setDeliverId(deliverHeader.getId());
		deliverDetail.setDeliverSeqNo(deliverHeader.getSeqNo());
		deliverDetail.setDeliverId(deliverHeader.getId());
		deliverDetail.setItemCode(goodsSku.getItemCode());
		deliverDetail.setSkuCode(goodsSku.getCode());
		deliverDetail.setSkuId(goodsSku.getId());
		deliverDetail.setSkuName(goodsSku.getName());		
		deliverDetail.setStatus(StatusEnum.ACTIVE.getStatus());
		deliverDetail.setActualPurchasePrice(goodsSku.getPurchasePrice());
		deliverDetail.setActualQuantify(1d);
		deliverDetail.setActualAmount(goodsSku.getPurchasePrice());
		deliverDetail.setSeq(deliverSequenceService.getDeliverDetailNextValue());
		UserModel userModel = this.getUserModelFromSession();
		AppUtil.setCreateUserInfo(userModel, deliverDetail);
		
		this.deliverDetailService.addModel(deliverDetail);	
		
		DeliverHeaderModel header = new DeliverHeaderModel();
		header.setId(deliverDetail.getDeliverId());
		deliverHeaderService.updateCalculateDeliverHeaderQuantifyAndAmount(header);

		
		this.addActionMessage("添加成功");
		return SUCCESS;
	}
	
	
	public String searchDeliverDetail(){
		/*DeliverDetailSearchModel deliverDetailSearchModel = new DeliverDetailSearchModel();
		deliverDetailSearchModel.setDeliverDetail(deliverDetail);		
		deliverDetailSearchModel.setPager(pager);		
		pager=deliverDetailService.getByNameLikePager(deliverDetailSearchModel);
		*/
		
		pager = deliverDetailService.getByNameLikePager(null, pager);
		return SUCCESS;
	}
	public String addDeliverDetailInit(){

		return SUCCESS;
	}
	
	public String updateDeliverDetail(){
		
		if(StringUtils.isEmpty(this.actionCommand)){
			deliverDetail = deliverDetailService.getByPK(deliverDetail.getId());	
		}else{
			UserModel userModel = this.getUserModelFromSession();
			AppUtil.setUpdateUserInfo(userModel, deliverDetail);
			deliverDetailService.updateModel(deliverDetail);
			this.addActionMessage("修改成功");
		}
		return SUCCESS;
	}
	
	public String deleteDeliverDetail(){
		deliverDetailService.deleteModel(deliverDetail);
		return SUCCESS;
	}
	
	
	public DeliverDetailModel getDeliverDetail() {
		return deliverDetail;
	}

	public void setDeliverDetail(DeliverDetailModel deliverDetail) {
		this.deliverDetail = deliverDetail;
	}
	
	@JSON
	public Pager<DeliverDetailModel> getPager() {
		return pager;
	}

	
	public void setPager(Pager<DeliverDetailModel> pager) {
		this.pager = pager;
	}

	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}
}
